#####################################################################
# Product:      KnowledgeEngine (Plone Edition)
#
# File:         SummarySimpleReport.py
#
# Copyright:    Copyright (c) 2005, Faulkner Technologies
#
# Author:       Darcy Clark
#
# Description:  This report generates a report that is equivalent to
#               a collection of SimpleReports
#
# See Also:     GenericObject, Component, Model, Section
#####################################################################

#####################################################################
## Knowledge Engine Library Imports
from Products.KnowledgeEngine.Common import *
from Products.KnowledgeEngine.Core import *
from Products.KnowledgeEngine.MultipleAssessmentReport import MultipleAssessmentReport, MultipleAssessmentReportInfo
from Products.KnowledgeEngine.PropertyDefinition import *
from Products.KnowledgeEngine.PropertyTypes import *
from Products.KnowledgeEngine.PropertyFormats import *
from Products.KnowledgeEngine.PropertyStructures import *
from Products.KnowledgeEngine.EventDefinition import *
from Products.KnowledgeEngine.WizardStepDefinition import *

#####################################################################
## Zope Library Imports
from Globals import DTMLFile
from App.ImageFile import ImageFile

#####################################################################
## Python Library Imports
# (none)

#####################################################################
## SummarySimpleReportInfo Class Definition
class SummarySimpleReportInfo(MultipleAssessmentReportInfo):
    """
        Defines the class info methods, defining information about
        the class without needing an instance of the class
    """
    
    #---------------------------------------------------------------#
    
    def getClassName(self):
        """
            Returns the name of the class for which 
            we are providing information.
        """
        return 'SummarySimpleReport'
        
    #---------------------------------------------------------------#
    
    def getDisplayName(self):
        """
            Return a friendly name for the class instances
            (this is used for displaying in lists etc)
        """
    
        return "Summary Simple Report"
        
    #---------------------------------------------------------------#
    
    def getIdentityType(self):
        """
            Return the identity type.  
        """
        
        return "SummarySimpleReport"
        
    #---------------------------------------------------------------#
    
    def getClassIcon(self):
        """
            Return the appropriate icon file for the class.
        """
        
        return ImageFile('skins/SummarySimpleReport_icon.gif', globals())
    
    #---------------------------------------------------------------#
    
    def onConfigureProperties(self):
        """
            Automatically called by the Knowledge Engine once
            the class has been loaded.  Use this method to register 
            class specific properties using the PropertyReflection interface.
            
            If this class is sub-classed, a call must be made to
            the super-class to ensure super-class properties are
            correctly registered
            
            NOTE: This method is called before ClassInfo.onAfterStartup()            
        """
        
        #initialise the super-class properties for reflection
        MultipleAssessmentReportInfo.onConfigureProperties(self)
    
    #---------------------------------------------------------------#
    
    def onConfigureEvents(self):
        """
            Automatically called by the Knowledge Engine once
            the class has been loaded.  Use this method to register 
            class specific events using the EventReflection interface.
            
            If this class is sub-classed, a call must be made to
            the super-class to ensure super-class properties are
            correctly registered
            
            NOTE: This method is called before ClassInfo.onAfterStartup()            
        """
                
        #initialise the super-class events for reflection
        MultipleAssessmentReportInfo.onConfigureEvents(self)
        
    #---------------------------------------------------------------#
    
#####################################################################
## SummarySimpleReport Class Definition
class SummarySimpleReport(MultipleAssessmentReport):
    """
        A class to represent a report consisting of graphs of
        component state
    """
    
    #---------------------------------------------------------------#
    
    #ABSTRACT TRANSIENT: a ClassInfo implementation instance to provide info about this class
    _CLASSINFO = SummarySimpleReportInfo()
    
    #---------------------------------------------------------------#
    
    def __init__(self, id, identity):
        "Constructor for the object"        
        MultipleAssessmentReport.__init__(self, id, identity)
        
    #---------------------------------------------------------------#
    
    def renderReportFor(self, genericObject, request):
        """
            Renders the report content for the specified generic object
            and the type of report
        """
        
        #add the specified genericObject to the request
        request['GENERICOBJECT'] = genericObject
        
        #dynamically determine the report page template for the specified object
        return eval("self.%s_%s(self, REQUEST=request)" % (genericObject.getClassInfo().getClassName(), self.getClassInfo().getClassName()))
        
    #---------------------------------------------------------------#

    def renderReport(self, request, wizardState, **args):
        """
            Renders the report content (called by Report.generate)
            
            NOTE: Override this method to perform specific
            report generation/rendering using the provided
            parameters
        """

        #get the assessment storage service... we'll need that to manage the assessment
        assessmentStorageService = getAssessmentStorageService(self)

        #get the current user that is rendering the report
        user = request["USER"]

        #get the assessment filter to use to filter out assessments from the session 
        #(this is typically set by the assessment explorer)
        assessmentFilter = wizardState.getPropertyValue("assessmentFilter")
        
        #determine the number of assessments that satisfy the filter
        nrAssessments = assessmentStorageService.getNrAssessmentsSatisfyingFilter(assessmentFilter, user)   
        request.set('NRASSESSMENTS', nrAssessments)

        #fetch the assessments
        assessments = assessmentStorageService.getAssessmentsSatisfyingFilter(assessmentFilter, user, sortBy="dateCreated")
        request.set('ASSESSMENTS', assessments)
        
        #call the super class render to do the work
        return MultipleAssessmentReport.renderReport(self, request, wizardState, **args)
        
    #---------------------------------------------------------------#

#####################################################################
## Class Initialisation
# Register Class Information with the Knowledge Engine
registerGenericObjectClass(SummarySimpleReport)
#####################################################################
